import logger from '../core/logger';
import commentData from '../model/comments/commentData';
import dataComment from '../model/comments/dataComment';
import { commentPostType } from '../model/common/enums';
import returnModel from '../model/common/returnModel';
import apiInfo from './core/apiInfo';
import apiProvider from './core/apiProvider';
import { clientBase } from './core/clientBase';
import dataInfo from './core/dataInfo';

export class commentClient extends clientBase {
  constructor() {
    super();
  }

  getChild(oId: string, emoteType: number, rpid: number, loadpage: number): Promise<returnModel<dataComment>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://api.bilibili.com/x/v2/reply/main", `&oid=${oId}&plat=2&pn=${loadpage}&ps=10&root=${rpid}&type=${emoteType}`, null, false);
        this.client.getResult(info).then((res) => {
          var data: dataComment = res;
          resolve(self.buildSuccessResult<dataComment>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<dataComment>(e));
      }
    });
  }

  send(data: commentData, text: string): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let content = '';
        switch (data.type) {
          case commentPostType.New: {
            let info = new dataInfo(`&type=${data.emoteType}&oid=${data.oid}&message=${encodeURIComponent(text)}`);
            content = info.toString();
            break;
          }
          case commentPostType.ReplyComment: {
            let info = new dataInfo(`&type=${data.emoteType}&oid=${data.oid}&message=${encodeURIComponent(text)}&root=${data.rpid}&parent=${data.rpid}`);
            content = info.toString();
            break;
          }
          case commentPostType.ReplyAt: {
            text = "回复 @" + data.userName + ":" + text;
            let info = new dataInfo(`&type=${data.emoteType}&oid=${data.oid}&message=${encodeURIComponent(text)}&root=${data.root}&parent=${data.rpid}`);
            content = info.toString();
            break;
          }
        }
        this.client.postResult("https://api.bilibili.com/x/v2/reply/add", content).then((res) => {
          resolve(self.buildSuccessResult<any>(res));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<any>(e));
      }
    });
  }

  delete(type: number, aId: string, rpid: number): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new dataInfo(`&type=${type}&oid=${aId}&rpid=${rpid}`);
        this.client.postResult("https://api.bilibili.com/x/v2/reply/del", info).then((res) => {
          resolve(self.buildSuccessResult<any>(res));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<any>(e));
      }
    });
  }

  get(commentType: number, aid: string, currentPage: number, sort: number): Promise<returnModel<dataComment>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://api.bilibili.com/x/v2/reply/main", `&oid=${aid}&plat=2&pn=${currentPage}&ps=20&sort=${sort}&type=${commentType}`, null, false);
        this.client.getResult(info).then((res) => {
          var data: dataComment = res;
          resolve(self.buildSuccessResult<dataComment>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<dataComment>(e));
      }
    });
  }

  like(commentType: number, aid: string, rpid: number, action: number): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new dataInfo(`&type=${commentType}&rpid=${rpid}&oid=${aid}&action=${(action == 0 ? 1 : 0)}`);
        this.client.postResult("https://api.bilibili.com/x/v2/reply/action", info).then((res) => {
          resolve(self.buildSuccessResult<any>(res));
        });
      } catch (e) {
        logger.error(e);
        resolve(self.buildExceptionResult<any>(e));
      }
    });
  }
}

export default new commentClient();